//
//  FoundViewController.m
//  DeliciousFood1
//
//  Created by brother on 15/10/19.
//  Copyright (c) 2015年 com.hxm. All rights reserved.
//

#import "FoundViewController.h"
#import "MenuViewController.h"


@interface FoundViewController ()<UIScrollViewDelegate>
/*轮播动画属性开始*/
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageContro;

@property (weak, nonatomic) IBOutlet UIScrollView *mainScroll;
@property (nonatomic,retain) NSTimer *timer;
@property (nonatomic,assign) int imageCount;
@property (nonatomic,retain) NSArray *dataArray;
/*轮播动画属性结束*/

@end

@implementation FoundViewController

/*轮播动画方法开始*/

@synthesize imageCount;

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray arrayWithObjects:@"1",@"2",@"3",@"4",@"5",nil];
    }
    return _dataArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.mainScroll.contentSize = CGSizeMake(self.view.frame.size.width, self.view.frame.size.height*2);
    
    self.imageCount = 5;
    
    CGFloat imageW = 405;
    
    CGFloat imageH = 244;
    
    CGFloat imageY = 0;
    
    for (int i = 0; i< imageCount; i++) {
        CGFloat imageX = i * imageW;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
        NSString *imageName = [NSString stringWithFormat:@"img_%02d",i+1];
        imageView.image = [UIImage imageNamed:imageName];
        [self.scrollView addSubview:imageView];
    }
    
    self.scrollView.contentSize = CGSizeMake(imageW *imageCount, 0);
    
    self.scrollView.pagingEnabled = YES;
    
    self.pageContro.numberOfPages = imageCount;
    
    [self addTimerToScrollView];
}

- (void)addTimerToScrollView {
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0F target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    // 将定时器 添加到UI主线程中进行解决问题
    [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)reomveTimerToScrollView {
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"%ld",self.pageContro.currentPage);
    if (self.scrollView == scrollView) {
        [self reomveTimerToScrollView];
    }
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (self.scrollView == scrollView) {
        [self addTimerToScrollView];
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.scrollView == scrollView) {
        CGFloat offSetX = scrollView.contentOffset.x;
        self.pageContro.currentPage = offSetX/self.scrollView.frame.size.width;
    }
}


- (void)nextPage {
    NSInteger page = self.pageContro.currentPage;
    if (page == imageCount -1) {
        page = 0;
        [self.scrollView setContentOffset:CGPointZero animated:YES];
    }else {
        page++;
        [self.scrollView setContentOffset:CGPointMake(page*self.scrollView.frame.size.width, 0) animated:YES];
    }
}

/*轮播动画方法结束*/

//点击特色菜类别并实现页面跳转
- (IBAction)click:(UIButton *)sender {
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MenuViewController *menu = [storyBoard instantiateViewControllerWithIdentifier:@"menu"];
    menu.type = sender.currentTitle;
    [self presentViewController:menu animated:YES completion:nil];
}


@end
